public class TestDemo2 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        int key = 3;
        int ret = binarySearch(arr, key);
        System.out.println(ret);
    }

    public static int binarySearch(int[] arr, int key){
        int left = 0;
        int right = arr.length-1;
        while(left <= right){
            int mid = (left + right)/2;
            if(arr[mid] < key){
                left = mid+1;
            }
            else if(arr[mid] > key){
                right = mid-1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
